package com.cocostudio.erpapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.cocostudio.erpapp.common.Debug;
import com.cocostudio.erpapp.utils.Constant;
import com.cocostudio.erpapp.utils.UIHelper;
import com.cocostudio.erpapp.utils.WebServiceUtils;
import com.cocostudio.erpapp.vo.WSRealTimeSalesResult;
import com.cocostudio.erpapp.web.CustomWebChromeClient;
import com.cocostudio.erpapp.R;
import com.google.zxing.client.android.CaptureActivity;
public class GetGoodsInfoActivity extends CustomFragmentActivity {
    private static final String TAG = "GetGoodsInfoActivity";
    private static View searchView;

    private WebView webView;

    private EditText search_product_name;
    private EditText search_product_size;
    private EditText search_product_code;
    private EditText search_product_barcode;

    private String showtype;
    private RadioGroup search_showtype_group;


    public static final int SCAN_CODE = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Debug.LOGD) {
            Log.d(TAG, "onCreate.");
        }
        super.onCreate(savedInstanceState);
        initView();

    }

    public void onScanBarCode(View view){
        Intent intent = new Intent(GetGoodsInfoActivity.this, CaptureActivity.class);
        startActivityForResult(intent, SCAN_CODE);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case SCAN_CODE:
            TextView scanResult = (TextView) findViewById(R.id.search_product_barcode);
            if (resultCode == RESULT_OK) {
                String result = data.getStringExtra("scan_result");
                scanResult.setText(result);
                onSearchClicked(null);
            } else if (resultCode == RESULT_CANCELED) {
                scanResult.setText("");
            }
            break;
        default:
            break;
        }
    }

    public void onSearchClicked(View view) {
        
            if ( (search_product_barcode.getText().toString().trim().length()==0) &&
                    (search_product_code.getText().toString().trim().length()==0)   
                    )
            {
                UIHelper.showAlert(this,"请确认商品信息输入正确");
                search_product_code.setFocusable(true);
                return;
            }
        
        
        try {
            pdg = ProgressDialog.show(GetGoodsInfoActivity.this, "温馨提示", "数据请求中，请稍后！");
            pdg.setCancelable(true);
            pdg.setCanceledOnTouchOutside(true);

            webView.loadUrl( Constant.urlprefix+ "getgoodsinfo.html");
            

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    public String getFromAssets(String fileName){
        String Result="";
        try {
             InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line="";
             Result="";
            while((line = bufReader.readLine()) != null)
                Result += line;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result;
    }

    private void initView() {

        setContentView(R.layout.activity_getgoods_info);

        setTitle(R.string.title_getgoods_info);

        setLeftButtonDrawable(R.drawable.btn_home);

        searchView = findViewById(R.id.search_component);


        //search_product_name = (EditText)findViewById(R.id.search_product_name);
        //search_product_size= (EditText)findViewById(R.id.search_product_size);
        search_product_code= (EditText)findViewById(R.id.search_product_code);
        search_product_barcode=(EditText)findViewById(R.id.search_product_barcode);

        //search_product_code.setText(  );
        //search_product_name.setText("兰扣");

        //search_product_size.setText("");

        this.showtype = "1";
        
        InitShowTypeUI();
        initWebViewUI();
        initShowTypeUI();
   }

    private void initShowTypeUI() {
        search_showtype_group = (RadioGroup)findViewById(R.id.showtype_radiogroup);
        search_showtype_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int btnid = group.getCheckedRadioButtonId();
                switch (btnid) {
                case R.id.showtype1:
                   GetGoodsInfoActivity.this.showtype ="1";//售价查询
                    break;
                case R.id.showtype2:
                    GetGoodsInfoActivity.this.showtype="2";//进价查询
                    break;
                default:
                    break;
                }
            }
        });
    }

    private void initWebViewUI() {
        webView = (WebView) findViewById(R.id.detail_result_webview);


        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(false);
        webView.setFocusableInTouchMode(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient( new CustomWebChromeClient());

        webView.addJavascriptInterface(this, "android");
    }

    private void InitShowTypeUI() {
//        search_showtype_spinner = (Spinner)findViewById(R.id.search_showtype_spinner);
//        String[] mshowItems = new String[]{"售价查询","进价查询"};
//
//        ArrayAdapter<String> _Adapter_showtype=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, mshowItems);
//        search_showtype_spinner.setAdapter(_Adapter_showtype);
//        search_showtype_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
//            
//                GetGoodsInfoActivity.this.showtype = Integer.toString(position+1);
//            
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent){
//                Log.d(GetGoodsInfoActivity.TAG, "select onNothing");
//            }
//        });
    }

    
    
    @JavascriptInterface
    public String getjson() {
        return WebServiceUtils.WS_GetGoodsInfo_json(
                search_product_code.getText().toString(), 
                search_product_barcode.getText().toString(),
                "",
                "",
                this.showtype);
    }

    @JavascriptInterface
    public String showtype() {
        return this.showtype;
    }
    
  

}
